﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuestionsAnswers.UI.Models;

namespace QuestionsAnswers.UI.Controllers
{
    public class RatingController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();
        //
        // GET: /Rating/ 
        [Authorize]
        public ActionResult Rating(string way, int id, string type, string returnUrl)
        {
            string ret;
            int rating = 0;
            bool success = false;
            User user = _db.Users.First(x => x.UserName == User.Identity.Name);
            IEnumerable<Rating> ratings = _db.Ratings.Where(x => x.TypeRatedId == id && x.TypeRated == type && x.UserId == user.UserId);
            if (ratings.Any())
            {
                if (ratings.First().Rate == way)
                {
                    ret = "You have already rated this " + type + " " + way;
                }
                else
                {
                    ratings.First().Rate = way;
                    ret = "You have successfully rated this " + type + " " + way;
                    rating = this.ChangeRating(id, type, way);
                    _db.Ratings.Remove(ratings.First());
                    _db.SaveChanges();
                    success = true;
                }
            }
            else
            {
                _db.Ratings.Add(new Rating { Rate = way, TypeRated = type, TypeRatedId = id, User = user, UserId = user.UserId });
                _db.SaveChanges();
                ret = "You have successfully rated this " + type + " " + way;
                rating = this.ChangeRating(id, type, way);
                success = true;

            }

            if (Request.IsAjaxRequest())
            {
                return Json(new { ReturnString = ret, Rating = rating, Success = success, Id = id,Type = type }, JsonRequestBehavior.AllowGet);
            }

            return View(new List<string> { ret, returnUrl });
        }

        private int ChangeRating(int id, string type, string way)
        {
            switch (type)
            {
                case ("Question"):
                    {
                        Question question = _db.Questions.Find(id);
                        question.QuestionRating = way == "up" ? question.QuestionRating + 1 : question.QuestionRating - 1;
                        _db.SaveChanges();
                        return question.QuestionRating;

                    }
                case ("Answer"):
                    {
                        Answer answer = _db.Answers.Find(id);

                        answer.AnswerRating = way == "up" ? answer.AnswerRating + 1 : answer.AnswerRating - 1;
                        _db.SaveChanges();
                        return answer.AnswerRating;

                    }

            } return 0;
        }
    }
}
